package com.example.live.view

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.common.mvi.BaseMviFragment
import com.example.live.R
import com.example.live.adapter.RankingUserAdapter
import com.example.live.databinding.FragmentUserBinding
import com.example.live.entity.UserEntity
import com.example.live.intent.LiveViewIntent
import com.example.live.state.LiveUiState
import com.example.live.vm.LiveViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class UserFragment : BaseMviFragment<FragmentUserBinding>() {
    private val vm:LiveViewModel by activityViewModels()
    private val userList = mutableListOf<UserEntity>()
    private lateinit var userAdapter: RankingUserAdapter
    private var positionIndex = 0
    override fun getLayoutId(): Int =R.layout.fragment_user

    override fun initViews() {
        userAdapter = RankingUserAdapter(userList)
        mDataBinding.userRv.apply {
            layoutManager = LinearLayoutManager(requireContext(),LinearLayoutManager.VERTICAL,false)
            adapter = userAdapter
                .apply {//点击事件
                    setOnClickListener(object :RankingUserAdapter.ItemOnClickListener{
                        override fun onClick(view: View, position: Int) {
                            super.onClick(view, position)
                            positionIndex = position//记录下标
                            mDataBinding.apply {
                                rankingRanking.setText("${position+1}")
                                rankingIcon.setBackgroundResource(R.drawable.ic_mine_mine)
                                rankingName.setText(userList[position].username)
                            }
                        }
                    })
                }
        }

        //查看商品
        mDataBinding.rankingLook.setOnClickListener {
            this@UserFragment.showToastMsg("此项目还未开发，请敬请期待！！！！")
        }
        //榜单规则
        mDataBinding.rankingRules.setOnClickListener {
            this@UserFragment.showToastMsg("此项目还未开发，请敬请期待！！！！")
        }
    }

    override suspend fun sendViewIntent() {
        super.sendViewIntent()
        vm.channel.send(LiveViewIntent.userLiveRoom)//商户意图
    }

    override suspend fun collectUIState() {
        super.collectUIState()
        vm.uiStateFlow.collect{
            when(it){
                is LiveUiState.userLiveRoomSuccess -> {//商户成功UI状态
                    userList.addAll(it.entity)
                    //数据
                    mDataBinding.rankingRanking.setText("${positionIndex+1}")
                    mDataBinding.rankingIcon.setBackgroundResource(R.drawable.ic_launcher_background)
                    mDataBinding.rankingName.setText(it.entity[positionIndex].username)
                    userAdapter.notifyDataSetChanged()
                }
                is LiveUiState.userLiveRoomFail -> this@UserFragment.showToastMsg(it.msg)
                else ->{}
            }
        }
    }

}